package com.ignoreexception.config;

import com.ignoreexception.filter.CustomerFilter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * 应用程序入口
 *
 * @param
 * @return
 * @author 杨宇健
 * @since 2022/12/26 19:00
 */
public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * 设置一个配置类,代替Spring的配置文件
     * @since 2022/12/26 20:00
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {//初始化Listener的WebApplicationContext读取的配置类
        //实际上做的就是这样的事情 new AnnotationConfigWebApplicationContext(RootConfiguration.class);
        return new Class[]{SpringConfiguration.class};
    }
    /**
     * 设置一个配置类,代替SpringMVC的配置文件
     * @since 2022/12/26 20:01
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {//初始化DispatcherServlet的WebApplicationContext读取的配置类
        //实际上做的就是这样的事情 new AnnotationConfigWebApplicationContext(WebMvcServletConfiguration.class);
        return new Class[]{WebMvcServletConfiguration.class};
    }
    /**
     * 设置SpringMVC的前端控制器 DispatcherServlet的url-pattern
     * @since 2022/12/26 20:01
     */
    @Override
    protected String[] getServletMappings() {// 配置DispatcherServlet的url-pattern → /
        return new String[]{"/"};
    }
    /**
     * 设置当前的过滤器
     * @since 2022/12/26 20:02
     */
    @Override
    protected Filter[] getServletFilters() {
        //创建编码过滤器
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        //创建处理请求方式的过滤器
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        return new Filter[]{characterEncodingFilter,hiddenHttpMethodFilter,new CustomerFilter()};
    }
}
